package cn.niit.letuseatdemo.controller.store;

import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.service.StoreService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: su_fuming
 * @CreateTime： 2022/11/2 19:08
 */
@Slf4j
@RequestMapping("/store")
@Controller
public class StoreController {

    @Autowired
    private StoreService storeService;
   /**
     * @Description: 商家信息管理（列表）页面的初始化及查询
     * @Author: su_fuming
     * @CreateTime: 2022/11/2 20:39
    * @param: model
    * @param: store
    * @return: java.lang.String
     **/

    @GetMapping
    public String index(Model model, Store store) {
        model.addAttribute("store", store);
        Store store1 = storeService.selectStoreByStoreId(1L);
        model.addAttribute("store1", store1);
        return "store/list";

    }

    @GetMapping("/categoryshop/{categoryId}")
    public String selectBycategory(Model model, @PathVariable("categoryId") Long categoryId) {
        List<Store> store1 = storeService.selectStoreListByCategoryId(categoryId);
        model.addAttribute("store1", store1);
        return "store1/list";

    }
/**
  * @Description: 接收用户请求，打开商家信息添加、修改页面
    * 添加商家信息、修改商家信息共用一个页面
  * @Author: su_fuming
  * @CreateTime: 2022/11/2 20:40
 * @param: model
 * @param: storeId
 * @return: java.lang.String
  **/

    @GetMapping("/{storeId}")
    public String saveIndex(Model model, @PathVariable("storeId") Long storeId) {
        log.info("要修改的ID = " + storeId);
        Store store = null;
        if (null != storeId && storeId > 0) { //值的修改
            store = storeService.selectStoreByStoreId(storeId);
        }else{  //添加方法
            store = new Store();
        }
        model.addAttribute("store", store);
        return "store/add";
    }

    /**
      * @Description: 新增保存商家信息
      * @Author: su_fuming
      * @CreateTime: 2022/11/2 20:43
     * @param: store
     * @return: java.lang.String
      **/

    @PostMapping
    public String addSave(Store store) {
        int rows = storeService.insertStore(store);
        if (rows <= 0) {
            return "store/add";
        }
        /**如果插入成功，则重定向至商家信息列表页面*/
        return "redirect:/store";
    }

    /**
      * @Description: 根据id修改商家信息
      * @Author: su_fuming
      * @CreateTime: 2022/11/2 20:45
     * @param: store
     * @return: java.lang.String
      **/

    @PutMapping
    public String editSave(Store store) {
        int rows = storeService.updateStore(store);
        if (rows <= 0) {
            return "store/add";
        }
        /**如果插入成功，则重定向至商家信息列表页面*/
        return "redirect:/store";
    }
    /**
      * @Description: 根据ID删除商家信息
      * @Author: su_fuming
      * @CreateTime: 2022/11/2 20:47
     * @param: storeId 
     * @return: java.lang.String
      **/

    @ResponseBody
    @DeleteMapping("/{storeId}")
    public String delete(@PathVariable("storeId") Long storeId){
        int rows =storeService.deleteStoreByStoreId(storeId);
        if (rows == 1){
            return "true";
        }else{
            return "false";
        }
    }
}
